<template>
	<view class="uni-container">
		<uni-notice-bar show-icon scrollable text="欢迎使用本公司设备,有问题请随时咨询客服13613814961(微信同号)" />
	

		<view class="uni-container" v-show="isShow">
			<uni-forms ref="form" :value="formData" validateTrigger="bind">
				<uni-forms-item name="imei" label="设备编号:">
					<view style="vertical-align: middle;font-size:16px;margin-top: 8px;">{{formData.imei}}</view>
				</uni-forms-item>
				<uni-forms-item name="name" label="设备名称:" required>
					<uni-easyinput placeholder="请为您的设备起个名字" v-model="formData.name"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="openid" label="微信号:" v-show="false">
					<uni-easyinput placeholder="微信号" v-model="formData.openid"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="interval" label="报警间隔(分钟):">
					<uni-number-box :min="2" :max="120" background="#2979FF" color="#fff"
						v-model="formData.interval"></uni-number-box>
				</uni-forms-item>
				<uni-forms-item name="num" label="总报警次数:">
					<uni-number-box :min="1" :max="6" background="#2979FF" color="#fff"
						v-model="formData.num"></uni-number-box>
				</uni-forms-item>
				<uni-forms-item name="tel1" label="报警电话1:">
					<uni-easyinput placeholder="请输入接收通知的电话号码" v-model="formData.tel1"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="tel2" label="报警电话2:">
					<uni-easyinput placeholder="请输入接收通知的电话号码" v-model="formData.tel2"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="tel3" label="报警电话3:">
					<uni-easyinput placeholder="请输入接收通知的电话号码" v-model="formData.tel3"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="iswx" label="微信通知(免费):">
					<switch :checked="formData.iswx" v-model="formData.iswx" @change="switchChangeWx" />
				</uni-forms-item>
				<uni-forms-item name="issms" label="短信通知(0.1元/次):">
					<switch :checked="formData.issms" v-model="formData.issms" @change="switchChangeSms" />
				</uni-forms-item>
				<uni-forms-item name="isvms" label="电话通知(0.1元/次):">
					<switch :checked="formData.isvms" v-model="formData.isvms" @change="switchChangeVms" />
				</uni-forms-item>



				<view class="uni-button-group">
					<button type="primary" class="uni-button" style="width: 70%;margin-left: 20%;"
						@click="submit">提交保存</button>
					<!-- 	<button class="uni-button" style="width: 100px;" @click="f_close">返回</button> -->

				</view>
			</uni-forms>

		</view>


	</view>

</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'a-device';
	export default {
		data() {
			return {
				imei: "",
				isShow: true,
				formData: {

				}
			}
		},

		onLoad: function(param) {
			this.imei = param.imei
		},
		async onReady(options = {}) {

         await db.collection(dbCollectionName)
					.where({
						imei: this.imei
					})
					.field("imei,name,openid,interval,num,iswx,issms,isvms,tel1,tel2,tel3")
					.get({
		getOne:true
	}).then(
	(res) => {
		const data = res.result.data
		if (data) {
			this.formData=data
	}
	}
	)

		},
		methods: {
			getOpenid() {
				alert("3333333333")
				uni.login({
					provider: "wxpay",
					success: function(res) {
						uniCloud.callFunction({
							name: 'a-get-openid',
							data: {
								code: res.code
							},
							success: (res) => {
								alert('Openid为', res.result)
							},
							fail: () => {
								alert('获取失败')
							}
						})
					},
				});
			}
,
		async	submit() {
			
			  uni.showLoading({
			    mask: true
			  })
			  this.$refs.form.validate().then((res) => {
			    return this.submitForm(res)
			  }).catch(() => {
			  }).finally(() => {
			    uni.hideLoading()
			  })
			
		},submitForm(value){
			
			  // 使用 clientDB 提交数据
			  return db.collection(dbCollectionName)
			  .where({
			  	imei: this.imei
			  }).update(value).then((res) => {
			    uni.showToast({
			      icon: 'none',
			      title: '修改成功'
			    })
			 this.getOpenerEventChannel().emit('refreshData')
			    setTimeout(() => uni.navigateBack(), 500)
			  }).catch((err) => {
			    uni.showModal({
			      content: err.message || '请求服务失败',
			      showCancel: false
			    })
			  })
			
		},
			switchChangeWx: function(event) {
				this.formData.iswx = event.detail.value; // 获取开关状态
			},
			switchChangeSms: function(event) {
				this.formData.issms = event.detail.value; // 获取开关状态
			},
			switchChangeVms: function(event) {
				this.formData.isvms = event.detail.value; // 获取开关状态
			}
		}
	}
</script>

<style>
	* {
		font-size: 18px;
		font-weight: bold;
		font-family: "微软雅黑";
	}

	::v-deep .uni-forms-item__label {
		width: 140px !important;
	}

	.uni-container {
		margin: 15px
	}

	switch {
		float: right
	}
</style>